﻿using Leeder.Logger;
using ScanConfirm.Business;
using ScanConfirm.Handle;
using ScanConfirm.Model;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ScanConfirmForm
{
    public partial class FrmHistory : Form
    {
        private LogOperate LogObj => Log.GetLogger(this.GetType());
        ScanConfirmHandler scanConfirmHandler;
        QueueInfoBLL queueInfoBLL = new QueueInfoBLL();
        CarTypeBLL carTypeBLL = new CarTypeBLL();
        int pageSize = 30;       //数据表每页默认行数
        public FrmHistory()
        {
            InitializeComponent();

            //全屏，覆盖任务栏
            if (this.WindowState == FormWindowState.Maximized)
            {
                this.WindowState = FormWindowState.Normal;
            }
            else
            {
                this.FormBorderStyle = FormBorderStyle.None;
                this.WindowState = FormWindowState.Maximized;
            }

            dgvHistory.ClearSelection();
            dgvHistory.AutoGenerateColumns = false;
            dgvHistory.ReadOnly = true;
            dgvHistory.AddColumn("车型", "VehicleModel");
            dgvHistory.AddColumn("连番", "Sequence");
            dgvHistory.AddColumn("VIN码", "VinCode");
            dgvHistory.AddColumn("品番", "PartsCode");
            dgvHistory.AddColumn("记号", "Mark");
            dgvHistory.AddColumn("扫描结果", "Status");
            dgvHistory.AddColumn("扫描时间", "CompleteDate");

            //设置开始时间为三天前
            this.dpStartDate.Value = DateTime.Now.AddDays(-3);
            this.dpEndDate.Value = DateTime.Now;

            //设置分页控件每页数量
            uiPagination1.PageSize = pageSize;
            uiPagination1.Style = UIStyle.Blue;
        }

        private void FrmHistory_Load(object sender, EventArgs e)
        {
            var carModelList = carTypeBLL.GetModelAll();
            if (carModelList == null)
                carModelList = new List<CarTypeModel>();
            carModelList.Insert(0, new CarTypeModel { CarName = "--ALL--" });
            cbVehicleModel.DataSource = carModelList;
            cbVehicleModel.DisplayMember = "CarName";
            cbVehicleModel.ValueMember = "CarName";
            cbVehicleModel.SelectedIndex = 0;
            Search();
        }

        /// <summary>
        /// 查询
        /// </summary>
        private void Search()
        {
            DateTime? startDate = dpStartDate.Value;
            DateTime? endDate = dpEndDate.Value;
            ////设置分页控件总数
            uiPagination1.TotalCount = queueInfoBLL.GetHistoryCountByWhere(cbVehicleModel.SelectedValue.ToString(), txtSequence.Text, txtVIN.Text, startDate, endDate);
            uiPagination1_PageChanged(null, null, 1, pageSize);
        }

        private void uiPagination1_PageChanged(object sender, object pagingSource, int pageIndex, int count)
        {
            try
            {
                DateTime? startDate = dpStartDate.Value;
                DateTime? endDate = dpEndDate.Value;
                dgvHistory.DataSource = queueInfoBLL.GetHistoryListByWhere(cbVehicleModel.SelectedValue.ToString(), txtSequence.Text, txtVIN.Text, startDate, endDate,pageIndex, count); 
                dgvHistory.Refresh();
            }
            catch (Exception ex)
            {
                LogObj.Error(ex.Message);
            }
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            Search();
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            this.dpStartDate.Value = DateTime.Now.AddDays(-3);
            this.dpEndDate.Value = DateTime.Now;
            cbVehicleModel.SelectedIndex = 0;
            txtSequence.Text = "";
            txtVIN.Text = "";
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void dgvHistory_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if (e.ColumnIndex == 5)
            {
                e.FormattingApplied = true;
                DataGridViewRow row = dgvHistory.Rows[e.RowIndex];
                if (row != null)
                {
                    if (row.Cells[e.ColumnIndex].Value.ToString() == "ok")
                    {
                        e.Value = string.Format("{0}",
                          "匹配OK");
                    }
                    else if (row.Cells[e.ColumnIndex].Value.ToString() == "pass")
                    {
                        e.Value = string.Format("{0}",
                      "强制放行");
                    }
                }
            }
        }
    }
}
